package com.yilin.tms.user.controller;

import com.yilin.commons.exception.BusinessException;
import com.yilin.tms.core.application.ReturnData;
import com.yilin.tms.core.commons.annotation.*;
import com.yilin.tms.core.commons.base.BaseCode;
import com.yilin.tms.core.commons.base.BaseController;
import com.yilin.tms.core.commons.base.BaseEntity;
import com.yilin.tms.core.commons.utils.ParamUtil;
import com.yilin.tms.core.entity.user.UserType;
import com.yilin.tms.core.entity.user.hatch.*;
import com.yilin.tms.core.entity.user.organize.Organize;
import com.yilin.tms.core.remote.ISystemRemote;
import com.yilin.tms.user.service.IHatchService;
import com.yilin.tms.user.service.IOrganizeService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.*;

@RestController
@RequestMapping("/user/hatch/")
public class HatchController extends BaseController {
    private static final String privateDeveloperPassword = "www.yilinwuliu.cn@yilin_xbr_app_private_key";

    @Resource
    ISystemRemote systemRemote;
    @Resource
    IHatchService hatchService;
    @Resource
    IOrganizeService organizeService;

    /**
     * TODO: 特殊接口
     * TODO: 任何人員請勿暴露此接口
     * 顶级管理员专属
     * 获取所有产品信息
     * 仅用于APP生成工具调取
     */
    @SkipLoginVerify
    @PostMapping(value = "getAllPlatProductHatch")
    public ReturnData<List<Map<String,Object>> > getAllPlatProductHatch(String developerPassword) {
        if (!privateDeveloperPassword.equals(developerPassword)) return jsonErrView("环境密码错误");
        List<HatchPlatProduct> productList = hatchService.getAllPlatProductHatch();
        List<Map<String,Object>> mapList = new ArrayList<>();
        for (HatchPlatProduct plat : productList) {
            Map<String, Object> map = new HashMap<>();
            map.put("productCode", plat.getProductCode());
            map.put("productName", plat.getProductName());
            map.put("hatchPlatProduct", plat);
            map.put("hatchTmsInterface", hatchService.getHatchTmsInterfaceByPlatId(plat.getPlatId()));
            map.put("hatchAlipay", hatchService.getHatchAlipayByPlatId(plat.getPlatId()));
            map.put("hatchWechat", hatchService.getHatchWechatByPlatId(plat.getPlatId()));
            map.put("hatchGaodeMap", hatchService.getHatchGaodeMapByPlatId(plat.getPlatId()));
            map.put("hatchBaiduMap", hatchService.getHatchBaiduMapByPlatId(plat.getPlatId()));
            map.put("hatchBaiduOcr", hatchService.getHatchBaiduOcrByPlatId(plat.getPlatId()));
            map.put("hatchBaiduFace", hatchService.getHatchBaiduFaceByPlatId(plat.getPlatId()));
            map.put("hatchJpush", hatchService.getHatchJpushByPlatId(plat.getPlatId()));
            mapList.add( map );
        }
        return jsonView(SUCCESS_CODE, "获取成功", mapList);
    }

    /**
     * 一键获取所有孵化配置
     * */
    @PostMapping(value = "getAllHatchConfig")
    @NotNull({"start", "limit"})
    public ReturnData<Object> getAllHatchConfig(@Explain("平台ID") String platId) {
        if (platId == null && getLoginUser().getUserType() == UserType.platformUser) platId = getLoginUser().getOrganizeId();
        if (platId == null) throw new BusinessException("您无需配置孵化信息");
        Map<String,Object> hatchMap = new HashMap<>();
        hatchMap.put("hatchProcess",hatchService.getHatchProcessByPlatId(platId));
        hatchMap.put("hatchAlipay",hatchService.getHatchAlipayByPlatId(platId));
        hatchMap.put("hatchEtc",hatchService.getHatchEtcByPlatId(platId));
        hatchMap.put("hatchJpush",hatchService.getHatchJpushByPlatId(platId));
        hatchMap.put("hatchSms",hatchService.getHatchSmsByPlatId(platId));
        hatchMap.put("hatchWechat",hatchService.getHatchWechatByPlatId(platId));
        hatchMap.put("hatchBaiduOcr",hatchService.getHatchBaiduOcrByPlatId(platId));
        hatchMap.put("hatchBaiduFace",hatchService.getHatchBaiduFaceByPlatId(platId));
        hatchMap.put("hatchBaiduMap",hatchService.getHatchBaiduMapByPlatId(platId));
        hatchMap.put("hatchGaodeMap",hatchService.getHatchGaodeMapByPlatId(platId));
        hatchMap.put("hatchPlatProduct",hatchService.getHatchPlatProductByPlatId(platId));
        hatchMap.put("hatchHyb",hatchService.getHatchHybMapperByPlatId(platId));
        hatchMap.put("hatchTmsInterface",hatchService.getHatchTmsInterfaceByPlatId(platId));
        hatchMap.put("pricingCommTruckTypeList", ParamUtil.isSuccess(systemRemote.getPricingCommTruckTypeList()));
        return jsonView(SUCCESS_CODE, "获取成功", hatchMap);
    }

    /**
     * 孵化进度
     * */
    @PostMapping(value = "getProcessHatch")
    public ReturnData<Object> getProcessHatch(@Explain("平台ID") String platId) {
        if (platId == null && getLoginUser().getUserType() == UserType.platformUser) platId = getLoginUser().getOrganizeId();
        if (platId == null) return jsonView(BaseCode.USER_TYPE_ERR,"您无需配置孵化信息");
        HatchProcess object = hatchService.getHatchProcessByPlatId(platId);
        return jsonView(SUCCESS_CODE, "获取成功", object);
    }


    /**
     * 获取平台ETC孵化配置
     */
    @PostMapping(value = "getEtcHatch")
    public ReturnData getEtcHatch(@Explain("平台ID") String platId) {
        if (platId == null && getLoginUser().getUserType() == UserType.platformUser) platId = getLoginUser().getOrganizeId();
        if (platId == null)return jsonView(BaseCode.USER_TYPE_ERR,"您无需配置孵化信息");
        BaseEntity object = hatchService.getHatchEtcByPlatId(platId);
        return jsonView(SUCCESS_CODE, "获取成功", object);
    }

    /**
     * 获取平台Jpush孵化配置
     */
    @PostMapping(value = "getJpushHatch")
    public ReturnData getJpushHatch(@Explain("平台ID") String platId) {
        if (platId == null && getLoginUser().getUserType() == UserType.platformUser) platId = getLoginUser().getOrganizeId();
        if (platId == null)return jsonView(BaseCode.USER_TYPE_ERR,"您无需配置孵化信息");
        BaseEntity object = hatchService.getHatchJpushByPlatId(platId);
        return jsonView(SUCCESS_CODE, "获取成功", object);
    }

    /**
     * 获取平台Sms孵化配置
     */
    @PostMapping(value = "getSmsHatch")
    @SkipLoginVerify
    public ReturnData getSmsHatch(@Explain("平台ID") String platId) {
        if (platId == null && getLoginUser().getUserType() == UserType.platformUser) platId = getLoginUser().getOrganizeId();
        if (platId == null)return jsonView(BaseCode.USER_TYPE_ERR,"您无需配置孵化信息");
        BaseEntity object = hatchService.getHatchSmsByPlatId(platId);
        return jsonView(SUCCESS_CODE, "获取成功", object);
    }

    /**
     * 获取平台孵化配置
     */
    @PostMapping(value = "getAlipayHatch")
    public ReturnData getAlipayHatch(@Explain("平台ID") String platId) {
        if (platId == null && getLoginUser().getUserType() == UserType.platformUser) platId = getLoginUser().getOrganizeId();
        if (platId == null) return jsonView(BaseCode.USER_TYPE_ERR,"您无需配置孵化信息");
        BaseEntity object = hatchService.getHatchAlipayByPlatId(platId);
        return jsonView(SUCCESS_CODE, "获取成功", object);
    }

    /**
     * 获取平台支付宝孵化配置
     */
    @PostMapping(value = "getAlipayHatchByCode")
    @SkipLoginVerify
    public ReturnData getAlipayHatchByCode(@Explain("产品码") String productCode) {
        HatchPlatProduct product = hatchService.getHatchPlatProductByProductCode(productCode);
        BaseEntity object = hatchService.getHatchAlipayByPlatId(product.getPlatId());
        return jsonView(SUCCESS_CODE, "获取成功", object);
    }

    /**
     * 获取平台微信支付孵化配置
     */
    @PostMapping(value = "getWechatHatch")
    public ReturnData getWechatHatch(@Explain("平台ID") String platId) {
        if (platId == null && getLoginUser().getUserType() == UserType.platformUser) platId = getLoginUser().getOrganizeId();
        if (platId == null) return jsonView(BaseCode.USER_TYPE_ERR,"您无需配置孵化信息");
        BaseEntity object = hatchService.getHatchWechatByPlatId(platId);
        return jsonView(SUCCESS_CODE, "获取成功", object);
    }

    /**
     * 获取平台微信孵化配置
     */
    @PostMapping(value = "getWechatHatchByCode")
    @SkipLoginVerify
    public ReturnData getWechatHatchByCode(@Explain("产品码") String productCode) {
        HatchPlatProduct product = hatchService.getHatchPlatProductByProductCode(productCode);
        BaseEntity object = hatchService.getHatchWechatByPlatId(product.getPlatId());
        return jsonView(SUCCESS_CODE, "获取成功", object);
    }

    /**
     * 获取平台百度OCR孵化配置
     */
    @PostMapping(value = "getBaiduOcrHatch")
    public ReturnData getBaiduOcrHatch(@Explain("平台ID") String platId) {
        if (platId == null && getLoginUser().getUserType() == UserType.platformUser) platId = getLoginUser().getOrganizeId();
        if (platId == null) return jsonView(BaseCode.USER_TYPE_ERR,"您无需配置OCR孵化信息");
        BaseEntity object = hatchService.getHatchBaiduOcrByPlatId(platId);
        return jsonView(SUCCESS_CODE, "获取成功", object);
    }

    /**
     * 获取平台百度人脸孵化配置
     */
    @PostMapping(value = "getBaiduFaceHatch")
    public ReturnData getBaiduFaceHatch(@Explain("平台ID") String platId) {
        if (platId == null && getLoginUser().getUserType() == UserType.platformUser) platId = getLoginUser().getOrganizeId();
        if (platId == null) return jsonView(BaseCode.USER_TYPE_ERR,"您无需配置Face孵化信息");
        BaseEntity object = hatchService.getHatchBaiduFaceByPlatId(platId);
        return jsonView(SUCCESS_CODE, "获取成功", object);
    }

    /**
     * 获取平台百度地图孵化配置
     */
    @PostMapping(value = "getBaiduMapHatch")
    @NotNull({"cfg"})
    public ReturnData getBaiduMapHatch(@Explain("平台ID") String platId) {
        if (platId == null && getLoginUser().getUserType() == UserType.platformUser) platId = getLoginUser().getOrganizeId();
        if (platId == null) return jsonView(BaseCode.USER_TYPE_ERR,"您无需配置百度地图孵化信息");
        BaseEntity object = hatchService.getHatchBaiduMapByPlatId(platId);
        return jsonView(SUCCESS_CODE, "获取成功", object);
    }

    /**
     * 获取平台高德孵化配置
     */
    @PostMapping(value = "getGaodeMapHatch")
    @SkipLoginVerify
    @NoLoginSecurity
    public ReturnData getGaodeMapHatch(@Explain("平台ID") String platId) {
        if (platId == null && getLoginUser().getUserType() == UserType.platformUser) platId = getLoginUser().getOrganizeId();
        if (platId == null) return jsonView(BaseCode.USER_TYPE_ERR,"您无需配置孵化信息");
        BaseEntity object = hatchService.getHatchGaodeMapByPlatId(platId);
        return jsonView(SUCCESS_CODE, "获取成功", object);
    }

    /**
     * 获取平台参数接口孵化配置
     */
    @PostMapping(value = "getPlatProductHatch")
    public ReturnData getPlatProductHatch(@Explain("平台ID") String platId) {
        if (platId == null && getLoginUser().getUserType() == UserType.platformUser) platId = getLoginUser().getOrganizeId();
        if (platId == null) return jsonView(BaseCode.USER_TYPE_ERR,"您无需配置孵化信息");
        BaseEntity  object = hatchService.getHatchPlatProductByPlatId(platId);
        return jsonView(SUCCESS_CODE, "获取成功", object);
    }

    /**
     * 获取平台参数接口孵化配置
     */
    @PostMapping(value = "getPlatProductHatchByCode")
    @SkipLoginVerify
    @NoLoginSecurity
    public ReturnData getPlatProductHatchByCode(@Explain("产品码") String productCode) {
        BaseEntity object = hatchService.getHatchPlatProductByProductCode(productCode);
        return jsonView(SUCCESS_CODE, "获取成功", object);
    }

    /**
     * 获取平台省运管孵化配置
     */
    @PostMapping(value = "getTmsInterfaceHatch")
    public ReturnData getTmsInterfaceHatch(@Explain("平台ID") String platId) {
        if (platId == null && getLoginUser().getUserType() == UserType.platformUser) platId = getLoginUser().getOrganizeId();
        if (platId == null) return jsonView(BaseCode.USER_TYPE_ERR,"您无需配置孵化信息");
        BaseEntity object = hatchService.getHatchTmsInterfaceByPlatId(platId);
        return jsonView(SUCCESS_CODE, "获取成功", object);
    }

    /**
     * 获取好运宝孵化配置
     */
    @PostMapping(value = "getHybHatch")
    public ReturnData getHybHatch(@Explain("平台ID") String platId) {
        if (platId == null && getLoginUser().getUserType() == UserType.platformUser) platId = getLoginUser().getOrganizeId();
        if (platId == null) return jsonView(BaseCode.USER_TYPE_ERR,"您无需配置孵化信息");
        BaseEntity object = hatchService.getHatchHybMapperByPlatId(platId);
        return jsonView(SUCCESS_CODE, "获取成功", object);
    }


    /**
     * 获取指定平台及车型计价规则
     */
    @PostMapping(value = "getCommTruckTypeRule")
    @NotNull({"commTruckTypeId"})
    public ReturnData<TruckTypeRuleMap> getCommTruckTypeRule(String platId, String commTruckTypeId) {
        if (platId == null && getLoginUser().getUserType() == UserType.platformUser) {
            platId = getLoginUser().getOrganizeId();
        }
        List<HatchPricingRule> ruleList = hatchService.getCommTruckTypeRule(platId, commTruckTypeId);
        TruckTypeRuleMap map = new TruckTypeRuleMap();
        for (HatchPricingRule rule : ruleList) {
            if (rule.isMatching()) map.setMatch(rule);
            else map.setCarrier(rule);
        }
        return jsonView(SUCCESS_CODE, "获取成功", map);
    }

    /**
     * 编辑计价规则
     */
    @PostMapping(value = "editCommTruckTypeRule")
    @UserTarget(UserType.platformUser)
    public ReturnData<Object> editCommTruckTypeRule(String platId, TruckTypeRuleMap truckTypeRuleMap) {
        if (platId == null && getLoginUser().getUserType() == UserType.platformUser) {
            platId = getLoginUser().getOrganizeId();
        }
        Organize organize = organizeService.getOrganizeById(platId);
        if (!organize.getSuperiorPlatOrganizeId().equals(getLoginUser().getOrganizeId())
                && !organize.getId().equals(getLoginUser().getOrganizeId())) {
            return jsonView(SUCCESS_CODE, "您无权操作");
        }
        if (truckTypeRuleMap.getCarrier() == null) return jsonView(FAIL_CODE, "没有找到承运规则");
        if (truckTypeRuleMap.getMatch() == null) return jsonView(FAIL_CODE, "没有找到撮合规则");

        HatchPricingRule carrier = truckTypeRuleMap.getCarrier();
        carrier.setMatching(false);
        if (carrier.getPlatId() == null) {
            carrier.setPlatId(getLoginUser().getOrganizeId());
            carrier.setPlatName(getLoginUser().getOrganizeName());
        }
        carrier.setUpdateUserId(getLoginUser().getId());
        carrier.setUpdateTime(new Date());
        hatchService.saveOrUpdateRule(carrier);
        HatchPricingRule match = truckTypeRuleMap.getMatch();
        match.setMatching(true);
        if (match.getPlatId() == null) {
            match.setPlatId(getLoginUser().getOrganizeId());
            match.setPlatName(getLoginUser().getOrganizeName());
        }
        match.setUpdateUserId(getLoginUser().getId());
        match.setUpdateTime(new Date());
        hatchService.saveOrUpdateRule(match);
        return jsonView(SUCCESS_CODE, "操作成功");
    }

    /**
     * 添加孵化HatchAlipay
     */
    @PostMapping(value = "editHatchAlipay")
    public ReturnData<Object> editHatchAlipay(HatchAlipay hatchAlipay) {
        if (hatchAlipay.getPlatId() == null && getLoginUser().getUserType() == UserType.platformUser) {
            hatchAlipay.setPlatId(getLoginUser().getOrganizeId());
        }
        Organize organize = organizeService.getOrganizeById(hatchAlipay.getPlatId());
        if (organize.getUserType() != UserType.platformUser) {
            throw new BusinessException("孵化对象错误");
        }
        if (!organize.getSuperiorPlatOrganizeId().equals(getLoginUser().getOrganizeId())
                && !organize.getId().equals(getLoginUser().getOrganizeId())) {
            return jsonView(SUCCESS_CODE, "无权操作");
        }
        hatchAlipay.setPlatName(organize.getOrganizeName());
        hatchAlipay.setUpdateUserId(getLoginUser().getId());
        hatchAlipay.setUpdateTime(new Date());
        hatchService.editHatchAlipay(hatchAlipay);
        return jsonView(SUCCESS_CODE, "操作成功");
    }

    /**
     * 添加孵化 HatchBaiduOcr
     */
    @PostMapping(value = "editHatchBaiduOcr")
    @NotNull({"apiKey", "appId", "secretKey"})
    public ReturnData<Object> editHatchBaiduOcr(HatchBaiduOcr hatchBaiduOcr) {
        if (hatchBaiduOcr.getPlatId() == null && getLoginUser().getUserType() == UserType.platformUser) {
            hatchBaiduOcr.setPlatId(getLoginUser().getOrganizeId());
        }
        Organize organize = organizeService.getOrganizeById(hatchBaiduOcr.getPlatId());
        if (organize.getUserType() != UserType.platformUser) {
            throw new BusinessException("孵化对象错误");
        }
        if (!organize.getSuperiorPlatOrganizeId().equals(getLoginUser().getOrganizeId())
                && !organize.getId().equals(getLoginUser().getOrganizeId())) {
            return jsonView(SUCCESS_CODE, "无权操作");
        }
        hatchBaiduOcr.setPlatName(organize.getOrganizeName());
        hatchBaiduOcr.setUpdateUserId(getLoginUser().getId());
        hatchBaiduOcr.setUpdateTime(new Date());
        hatchService.editHatchBaiduOcr(hatchBaiduOcr);
        return jsonView(SUCCESS_CODE, "操作成功");
    }

    /**
     * 添加孵化 HatchBaiduFACE
     */
    @PostMapping(value = "editHatchBaiduFace")
    @NotNull({"apiKey", "appId", "secretKey"})
    public ReturnData<Object> editHatchBaiduFace(HatchBaiduFace hatchBaiduFace) {
        if (hatchBaiduFace.getPlatId() == null && getLoginUser().getUserType() == UserType.platformUser) {
            hatchBaiduFace.setPlatId(getLoginUser().getOrganizeId());
        }
        Organize organize = organizeService.getOrganizeById(hatchBaiduFace.getPlatId());
        if (organize.getUserType() != UserType.platformUser) {
            throw new BusinessException("孵化对象错误");
        }
        if (!organize.getSuperiorPlatOrganizeId().equals(getLoginUser().getOrganizeId())
                && !organize.getId().equals(getLoginUser().getOrganizeId())) {
            return jsonView(SUCCESS_CODE, "无权操作");
        }
        hatchBaiduFace.setPlatName(organize.getOrganizeName());
        hatchBaiduFace.setUpdateUserId(getLoginUser().getId());
        hatchBaiduFace.setUpdateTime(new Date());
        hatchService.editHatchBaiduFace(hatchBaiduFace);
        return jsonView(SUCCESS_CODE, "操作成功");
    }


    /**
     * 添加孵化 HatchBaiduMap
     */
    @PostMapping(value = "editHatchBaiduMap")
    @NotNull({"bdAppKey", "serviceId"})
    public ReturnData<Object> editHatchBaiduMap(HatchBaiduMap hatchBaiduMap) {
        if (hatchBaiduMap.getPlatId() == null && getLoginUser().getUserType() == UserType.platformUser) {
            hatchBaiduMap.setPlatId(getLoginUser().getOrganizeId());
        }
        Organize organize = organizeService.getOrganizeById(hatchBaiduMap.getPlatId());
        if (organize.getUserType() != UserType.platformUser) {
            throw new BusinessException("孵化对象错误");
        }
        if (!organize.getSuperiorPlatOrganizeId().equals(getLoginUser().getOrganizeId())
                && !organize.getId().equals(getLoginUser().getOrganizeId())) {
            return jsonView(SUCCESS_CODE, "无权操作");
        }
        hatchBaiduMap.setPlatName(organize.getOrganizeName());
        hatchBaiduMap.setUpdateUserId(getLoginUser().getId());
        hatchBaiduMap.setUpdateTime(new Date());
        hatchService.editHatchBaiduMap(hatchBaiduMap);
        return jsonView(SUCCESS_CODE, "操作成功");
    }

    /**
     * 添加孵化 HatchGaodeMap
     */
    @PostMapping(value = "editHatchGaodeMap")
    @NotNull({"gdWebKey", "gdSig"})
    public ReturnData<Object> editHatchGaodeMap(HatchGaodeMap hatchGaodeMap) {
        if (hatchGaodeMap.getPlatId() == null && getLoginUser().getUserType() == UserType.platformUser) {
            hatchGaodeMap.setPlatId(getLoginUser().getOrganizeId());
        }
        Organize organize = organizeService.getOrganizeById(hatchGaodeMap.getPlatId());
        if (organize.getUserType() != UserType.platformUser) {
            throw new BusinessException("孵化对象错误");
        }
        if (!organize.getSuperiorPlatOrganizeId().equals(getLoginUser().getOrganizeId())
                && !organize.getId().equals(getLoginUser().getOrganizeId())) {
            return jsonView(SUCCESS_CODE, "无权操作");
        }
        hatchGaodeMap.setPlatName(organize.getOrganizeName());
        hatchGaodeMap.setUpdateUserId(getLoginUser().getId());
        hatchGaodeMap.setUpdateTime(new Date());
        hatchService.editHatchGaodeMap(hatchGaodeMap);
        return jsonView(SUCCESS_CODE, "操作成功");
    }

    /**
     * 添加孵化 HatchEtc
     */
    @PostMapping(value = "editHatchEtc")
    @NotNull({"appKey", "appSecret", "taxplayerCode", "url"})
    public ReturnData<Object> editHatchEtc(HatchEtc hatchEtc) {
        if (hatchEtc.getPlatId() == null && getLoginUser().getUserType() == UserType.platformUser) {
            hatchEtc.setPlatId(getLoginUser().getOrganizeId());
        }
        Organize organize = organizeService.getOrganizeById(hatchEtc.getPlatId());
        if (organize.getUserType() != UserType.platformUser) {
            throw new BusinessException("孵化对象错误");
        }
        if (!organize.getSuperiorPlatOrganizeId().equals(getLoginUser().getOrganizeId())
                && !organize.getId().equals(getLoginUser().getOrganizeId())) {
            return jsonView(SUCCESS_CODE, "无权操作");
        }
        hatchEtc.setPlatName(organize.getOrganizeName());
        hatchEtc.setUpdateUserId(getLoginUser().getId());
        hatchEtc.setUpdateTime(new Date());
        hatchService.editHatchEtc(hatchEtc);
        return jsonView(SUCCESS_CODE, "操作成功");
    }

    /**
     * 添加孵化 HatchJpush
     */
    @PostMapping(value = "editHatchJpush")
    @NotNull({"shipperAppKey", "shipperMasterSecret", "truckerAppKey", "truckerMasterSecret"})
    public ReturnData<Object> editHatchJpush(HatchJpush hatchJpush) {
        if (hatchJpush.getPlatId() == null && getLoginUser().getUserType() == UserType.platformUser) {
            hatchJpush.setPlatId(getLoginUser().getOrganizeId());
        }
        Organize organize = organizeService.getOrganizeById(hatchJpush.getPlatId());
        if (organize.getUserType() != UserType.platformUser) {
            throw new BusinessException("孵化对象错误");
        }
        if (!organize.getSuperiorPlatOrganizeId().equals(getLoginUser().getOrganizeId())
                && !organize.getId().equals(getLoginUser().getOrganizeId())) {
            return jsonView(SUCCESS_CODE, "无权操作");
        }
        hatchJpush.setPlatName(organize.getOrganizeName());
        hatchJpush.setUpdateUserId(getLoginUser().getId());
        hatchJpush.setUpdateTime(new Date());
        hatchService.editHatchJpush(hatchJpush);
        return jsonView(SUCCESS_CODE, "操作成功");
    }

    /**
     * 添加孵化 HatchSms
     */
    @PostMapping(value = "editHatchSms")
    @NotNull({"sendAccount", "sendPassword", "sendUrl"})
    public ReturnData<Object> editHatchSms(HatchSms hatchSms) {
        if (hatchSms.getPlatId() == null && getLoginUser().getUserType() == UserType.platformUser) {
            hatchSms.setPlatId(getLoginUser().getOrganizeId());
        }
        Organize organize = organizeService.getOrganizeById(hatchSms.getPlatId());
        if (organize.getUserType() != UserType.platformUser) {
            throw new BusinessException("孵化对象错误");
        }
        if (!organize.getSuperiorPlatOrganizeId().equals(getLoginUser().getOrganizeId())
                && !organize.getId().equals(getLoginUser().getOrganizeId())) {
            return jsonView(SUCCESS_CODE, "无权操作");
        }
        hatchSms.setPlatName(organize.getOrganizeName());
        hatchSms.setUpdateUserId(getLoginUser().getId());
        hatchSms.setUpdateTime(new Date());
        hatchService.editHatchSms(hatchSms);
        return jsonView(SUCCESS_CODE, "操作成功");
    }

    /**
     * 添加孵化 editPlatProduct
     */
    @PostMapping(value = "editHatchPlatProduct")
    @NotNull({"productCode"})
    public ReturnData<Object> editHatchPlatProduct(HatchPlatProduct hatchPlatProduct) {
        if (hatchPlatProduct.getPlatId() == null && getLoginUser().getUserType() == UserType.platformUser) {
            hatchPlatProduct.setPlatId(getLoginUser().getOrganizeId());
        }
        Organize organize = organizeService.getOrganizeById(hatchPlatProduct.getPlatId());
        if (organize.getUserType() != UserType.platformUser) {
            throw new BusinessException("孵化对象错误");
        }
        if (!organize.getSuperiorPlatOrganizeId().equals(getLoginUser().getOrganizeId())
                && !organize.getId().equals(getLoginUser().getOrganizeId())) {
            return jsonView(SUCCESS_CODE, "无权操作");
        }
        if (hatchPlatProduct.getProductCode()==null) return jsonErrView("请填写产品码");
        HatchPlatProduct byCode = hatchService.getHatchPlatProductByProductCode(hatchPlatProduct.getProductCode());
        if (byCode!=null && !byCode.getId().equals(hatchPlatProduct.getId())) return jsonErrView("产品码已存在，请适当修改");
        hatchPlatProduct.setPlatName(organize.getOrganizeName());
        hatchPlatProduct.setUpdateUserId(getLoginUser().getId());
        hatchPlatProduct.setUpdateTime(new Date());
        hatchService.editPlatProduct(hatchPlatProduct);
        return jsonView(SUCCESS_CODE, "操作成功");
    }

    /**
     * 添加孵化 HatchTmsInterface
     */
    @PostMapping(value = "editHatchTmsInterface")
    @NotNull({"businessLicenseNo", "transBusinessLicenseNo", "ms2KeyUrl", "tmsDataUrl", "tmsLoginUrl", "tmsUsername", "tmsPassword",
            "senderCode", "tmsIosAppId", "tmsIosAppSecurity", "tmsAndroidAppId", "tmsAndroidAppSecurity", "tmsSdkEnvironment"})
    public ReturnData<Object> editHatchTmsInterface(HatchTmsInterface hatchTmsInterface) {
        if (hatchTmsInterface.getPlatId() == null && getLoginUser().getUserType() == UserType.platformUser) {
            hatchTmsInterface.setPlatId(getLoginUser().getOrganizeId());
        }
        Organize organize = organizeService.getOrganizeById(hatchTmsInterface.getPlatId());
        if (organize.getUserType() != UserType.platformUser) {
            throw new BusinessException("孵化对象错误");
        }
        if (!organize.getSuperiorPlatOrganizeId().equals(getLoginUser().getOrganizeId())
                && !organize.getId().equals(getLoginUser().getOrganizeId())) {
            return jsonView(SUCCESS_CODE, "无权操作");
        }
        hatchTmsInterface.setPlatName(organize.getOrganizeName());
        hatchTmsInterface.setUpdateUserId(getLoginUser().getId());
        hatchTmsInterface.setUpdateTime(new Date());
        hatchService.editHatchTmsInterface(hatchTmsInterface);
        return jsonView(SUCCESS_CODE, "操作成功");
    }

    /**
     * 添加孵化 HatchWechat
     */
    @PostMapping(value = "editHatchWechat")
    public ReturnData<Object> editHatchWechat(HatchWechat hatchWechat) {
        if (hatchWechat.getPlatId() == null && getLoginUser().getUserType() == UserType.platformUser) {
            hatchWechat.setPlatId(getLoginUser().getOrganizeId());
        }
        Organize organize = organizeService.getOrganizeById(hatchWechat.getPlatId());
        if (organize.getUserType() != UserType.platformUser) {
            throw new BusinessException("孵化对象错误");
        }
        if (!organize.getSuperiorPlatOrganizeId().equals(getLoginUser().getOrganizeId())
                && !organize.getId().equals(getLoginUser().getOrganizeId())) {
            return jsonView(SUCCESS_CODE, "无权操作");
        }
        hatchWechat.setPlatName(organize.getOrganizeName());
        hatchWechat.setUpdateUserId(getLoginUser().getId());
        hatchWechat.setUpdateTime(new Date());
        hatchService.editHatchWechat(hatchWechat);
        return jsonView(SUCCESS_CODE, "操作成功");
    }

    /**
     * 添加孵化 HatchProcess
     */
    @PostMapping(value = "editHatchProcess")
    public ReturnData<Object> editHatchProcess(HatchProcess hatchProcess) {
        if (hatchProcess.getPlatId() == null && getLoginUser().getUserType() == UserType.platformUser) {
            hatchProcess.setPlatId(getLoginUser().getOrganizeId());
        }
        Organize organize = organizeService.getOrganizeById(hatchProcess.getPlatId());
        if (organize.getUserType() != UserType.platformUser) {
            throw new BusinessException("孵化对象错误");
        }
        if (!organize.getSuperiorPlatOrganizeId().equals(getLoginUser().getOrganizeId())
                && !organize.getId().equals(getLoginUser().getOrganizeId())) {
            return jsonView(SUCCESS_CODE, "无权操作");
        }
        hatchProcess.setPlatName(organize.getOrganizeName());
        hatchProcess.setUpdateUserId(getLoginUser().getId());
        hatchProcess.setUpdateTime(new Date());
        hatchService.editHatchProcess(hatchProcess);
        return jsonView(SUCCESS_CODE, "操作成功");
    }

    /**
     * 添加孵化 HatchHab
     */
    @PostMapping(value = "editHatchHyb")
    public ReturnData<Object> editHatchHyb(HatchHyb hatchHyb) {
        if (hatchHyb.getPlatId() == null && getLoginUser().getUserType() == UserType.platformUser) {
            hatchHyb.setPlatId(getLoginUser().getOrganizeId());
        }
        Organize organize = organizeService.getOrganizeById(hatchHyb.getPlatId());
        if (organize.getUserType() != UserType.platformUser) {
            throw new BusinessException("孵化对象错误");
        }
        if (!organize.getSuperiorPlatOrganizeId().equals(getLoginUser().getOrganizeId())
                && !organize.getId().equals(getLoginUser().getOrganizeId())) {
            return jsonView(SUCCESS_CODE, "无权操作");
        }
        hatchHyb.setPlatName(organize.getOrganizeName());
        hatchHyb.setUpdateUserId(getLoginUser().getId());
        hatchHyb.setUpdateTime(new Date());
        hatchService.editHatchHyb(hatchHyb);
        return jsonView(SUCCESS_CODE, "操作成功");
    }
}